package com.devicetracker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DeviceTrackerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button registerButton = (Button)findViewById(R.id.registerButton);
        registerButton.setOnClickListener(new Button.OnClickListener(){
			@Override
	       	public void onClick(View arg0) {
				EditText emailId = (EditText)findViewById(R.id.emailAddress);
				EditText password = (EditText)findViewById(R.id.password);
				User user = User.getUser();
				user.setEmail(emailId.getText().toString());
				user.setPassword(password.getText().toString());
//				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/id"));
//				startActivity(browserIntent);
				Thread apiThread = new Thread(new Runnable(){
		            @Override
		            public void run() {
		            	ApiInvoker apiInvoker = new ApiInvoker();
		            	apiInvoker.checkIfDeviceStolen();
		            }
				});
				apiThread.start();
				try {
					apiThread.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Toast.makeText(getApplicationContext(), "Congratulations!! You have registered.", Toast.LENGTH_SHORT);
			}
        });
        
    }
    
    public static String convertStreamToString(InputStream is)
    {
     BufferedReader reader = new BufferedReader(new InputStreamReader(is));
     StringBuilder sb = new StringBuilder();

     String line = null;
     try {
      while ((line = reader.readLine()) != null) {
       sb.append(line + "\n");
      }
     }
     catch (IOException e) {
      Log.e("PHP Client","Error : "+e.getMessage());
     }
     finally {
      try {
       is.close();
      } catch (IOException e1) {
       Log.e("PHP Client","Error : "+e1.getMessage());
      }
     }
     return sb.toString();
    }
}